#include "studentterminal.h"
#include "ui_studentterminal.h"


#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QHostAddress>
#include <QJsonArray>
#include <QMessageBox>
#include <QDateTime>
#include <QLayout>
#include <QGridLayout>
#include "subjectcollection.h"

StudentTerminal::StudentTerminal(QWidget *parent,NetworkManager *networkManager) :
    QMainWindow(parent),
    ui(new Ui::StudentTerminal)
{
    ui->setupUi(this);

    setWindowTitle("考试界面");

    /* 剩余时间 */
    m_remainTime = 2 * 60 * 60;
    // "HH:MM:SS" format
    ui->lcdNumber_time->setDigitCount(8);
    // 设置显示数字的颜色和背景颜色
    ui->lcdNumber_time->setStyleSheet("QLCDNumber {"
                                 "background-color: black;"
                                 "color: green;"
                                 "border-radius: 5px;"
                                 "padding: 10px;"
                                 "}"
                                 );

    /* 创建定时器对象(剩余时间刷新定时器) */
    m_remainTimer = new QTimer(this);
    /* 设置1S 时间间隔 */
    m_remainTimer->setInterval(1000); // 1 second interval

    m_screenTimer = new QTimer(this);
    /* 设置5S 时间间隔 */
    m_screenTimer->setInterval(60000); // 60 second intervals

    /* 加载相机模块启动 */
    loadCameraModuleStart();

    socket.connectToHost("192.168.5.21",8080); // 连接服务器

    /* 默认样式风格 */
    m_defultStyleSheet = QString("QPushButton {"
                                "background-color: #888888;"
                                "color: white;"
                                "border-radius: 5px;"
                                "padding: 10px;"
                                "}"
                                "QPushButton:hover {"
                                "background-color: #2980b9;"  // 悬停时的背景颜色
                                "}"
                                "QPushButton:pressed {"
                                "background-color: #1abc9c;"  // 按下时的背景颜色
                                "}");


    /* 设置交卷按钮的样式 */
    ui->pushButton_over->setStyleSheet("QPushButton {"
                                 "background-color: #1abc9c;"
                                 "color: white;"
                                 "border-radius: 5px;"
                                 "padding: 10px;"
                                 "}"
                                 "QPushButton:hover {"
                                 "background-color: #2980b9;"  // 悬停时的背景颜色
                                 "}"
                                 "QPushButton:pressed {"
                                 "background-color: #434343;"  // 按下时的背景颜色
                                 "}");

    /* 初始化题目 - 从服务器中获取 */
    // loadRemoteExamSubject();

    /* 初始化当前题号为1 */
    m_currentQuestionId = 1;

    m_subject = new subjectcollection(ui->widget_subject);
    m_quesAnswer = new questioncollect(ui->widget_question);

    /* 信号和槽绑定 */
#if 0
    /* 题号按钮 */
    connect(m_subject, &subjectcollection::whichOneClick, this, &StudentTerminal::showPaperSubjectSlot);
    /* 选项按钮 */
    connect(m_quesAnswer, &questioncollect::whichOneClick, this, &StudentTerminal::selectAnswerSlot);
    /* 交卷 */
    connect(ui->pushButton_over, &QPushButton::clicked, this, &StudentTerminal::submitExamSlot);
    /* 定时截图定时器 */
     connect(m_screenTimer, &QTimer::timeout, this, &StudentTerminal::captureImageSlot);
    /* 剩余时间定时器 */
    connect(m_remainTimer, &QTimer::timeout, this, &StudentTerminal::remainTimeRefreshSlot);
    /* 定时器2超时 */
    connect(m_screenTimer, &QTimer::timeout, this, &StudentTerminal::imageCaptureSlot);
    /* 截图保存图片 */
    connect(m_imageCapture, &QCameraImageCapture::imageCaptured, this, &StudentTerminal::saveImageSlot);
    /* 等待题目加载好之后再调用显示题目 */
    connect(this, &StudentTerminal::loadSubjectCompleted, this, &StudentTerminal::loadCompleteSlot);
#endif

    /* 初始化总分为0. */
    m_totalScore = 0;

    /* 启动相机 */
    m_camera->start();

    /* 启动定时器 */
    m_remainTimer->start();
    m_screenTimer->start();
}

/* 加载相机模块启动 */
void StudentTerminal::loadCameraModuleStart()
{
    /* 相机部分 */
    /* 创建摄像机对象 */
    m_camera = new QCamera(QCameraInfo::defaultCamera());

    /* 创建取景器 */
    m_viewFinder = new QCameraViewfinder(this);
    /* 设置取景器大小 */
    m_viewFinder->setFixedSize(400, 270);

    /* 将拍摄镜头放到widget_camera框中 */
    QVBoxLayout * layout = new QVBoxLayout(ui->widget_camera);
    layout->addWidget(m_viewFinder);
    ui->widget_camera->setLayout(layout);

    /* 将取景器安装到相机上 */
    m_camera->setViewfinder(m_viewFinder);
    /* 创建图像捕捉对象 */
    m_imageCapture = new QCameraImageCapture(m_camera);
}

StudentTerminal::~StudentTerminal()
{
    delete ui;
}
